/**
 * @name 获取本周七天日期
 * @description: 传当天日期，返回本周7天日期
 * @param {day:String} 当天日期 格式:yyyy-mm-dd
 * @return 
 */

export const getWeek = (day) => {
	function formDateNum(num){
		let str = String(num)
		return str.length >= 2 ? str : '0'+ str
	}

	function getWeekDay(dateString) {
		let dateStringReg = /^\d{4}[/-]\d{1,2}[/-]\d{1,2}$/;
		if (dateString.match(dateStringReg)) {
			let presentDate = new Date(dateString),
				today = presentDate.getDay() !== 0 ? presentDate.getDay() : 7;
			return Array.from(new Array(7), function(val, index) {
				return formatDate(new Date(presentDate.getTime() - (today - index - 1) * 24 * 60 * 60 *
					1000));
			});
		} else {
			throw new Error('dateString should be like "yyyy-mm-dd" or "yyyy/mm/dd"');
		}

		function formatDate(date) {
			return date.getFullYear() + '-' + (formDateNum(date.getMonth() + 1)) + '-' + formDateNum(date.getDate());
		}
	}

	var data = getWeekDay(day);

	var arr = [];
	var weekday = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"];

	data.map((val, index) => {
		arr.push({
			id: index,
			name: weekday[index],
			job: val
		})
	});
	return arr
}
